package com.eeepay.eeepay_v2.model;

import com.eeepay.eeepay_v2.util.SPUtils;
import com.eeepay.eeepay_v2.util.SerializeUtils;
import com.eeepay.v2_library.log.LogUtils;

import java.io.Serializable;

/**
 * Created by zw on 2016/3/3 0003.
 *  登陆的用户信息以及信息保存
 */
public class UserInfo implements Serializable{

    private static UserInfo userInfo;

    private String userNo;//用户编号
    private String agentNo;//代理商编号
    private String agentNode;//代理商结点
    private String agentName;
    private String userName;//用户名称
    private String role;//角色   1为管理员   0为销售员
    private String phone;
    private String oneAgentNo;//一级代理商编号
    private String agentLevel;//代理商级别
    private String authorities;//权限集合
    private String handle;// 为0时代表该登录管理员用户不可更改销售员不可设置状态为无效。(管理员才下发此字段)

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getAgentNode() {
        return agentNode;
    }

    public void setAgentNode(String agentNode) {
        this.agentNode = agentNode;
    }

    public String getAgentNo() {
        return agentNo;
    }

    public void setAgentNo(String agentNo) {
        this.agentNo = agentNo;
    }

    public String getUserNo() {
        return userNo;
    }

    public void setUserNo(String userNo) {
        this.userNo = userNo;
    }

    public String getOneAgentNo() {
        return oneAgentNo;
    }

    public void setOneAgentNo(String oneAgentNo) {
        this.oneAgentNo = oneAgentNo;
    }

    public String getAgentLevel() {
        return agentLevel;
    }

    public void setAgentLevel(String agentLevel) {
        this.agentLevel = agentLevel;
    }

    public String getAuthorities() {
        return authorities;
    }

    public void setAuthorities(String authorities) {
        this.authorities = authorities;
    }

    public static UserInfo getInstance(){
        if(userInfo == null){
            userInfo = new UserInfo();
        }
        return userInfo;
    }

    private UserInfo(){}


    public String getAgentName() {
        return agentName;
    }

    public void setAgentName(String agentName) {
        this.agentName = agentName;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getHandle() {
        return handle;
    }

    public void setHandle(String handle) {
        this.handle = handle;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "userNo='" + userNo + '\'' +
                ", agentNo='" + agentNo + '\'' +
                ", agentNode='" + agentNode + '\'' +
                ", agentName='" + agentName + '\'' +
                ", userName='" + userName + '\'' +
                ", role='" + role + '\'' +
                ", phone='" + phone + '\'' +
                ", oneAgentNo='" + oneAgentNo + '\'' +
                ", agentLevel='" + agentLevel + '\'' +
                ", authorities='" + authorities + '\'' +
                ", handle='" + handle + '\'' +
                '}';
    }

    /**
     * 保存用户信息到本地
     */
    public static void saveUserInfo(){
        if(userInfo != null){
            String serialize = SerializeUtils.serialize(userInfo);//序列化
            SPUtils.saveString(SPUtils.USER_INFO, serialize);
        }else{
            LogUtils.d("保存用户信息失败");
        }
    }

    /**
     * 从本地获得保存的用户信息
     * @return
     */
    public static UserInfo getUserInfo2SP(){
        if(userInfo == null){
            String infoStr = SPUtils.getString(SPUtils.USER_INFO);
            userInfo = (UserInfo) SerializeUtils.deSerialization(infoStr);//反序列化
        }
        return userInfo;
    }

    /**
     * 删除用户信息
     */
    public static void removeUserInfo(){
        SPUtils.removeHistory(SPUtils.USER_INFO);
    }
}
